package com.data.modules.online.cgreport.util;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.data.modules.online.cgform.util.CgformUtil;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.expression.CaseExpression;
import net.sf.jsqlparser.expression.DateValue;
import net.sf.jsqlparser.expression.DoubleValue;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.Function;
import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.TimeKeyExpression;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.statement.select.*;

public class CgReportSqlFiledParseUtils {
    public static Map<String, Object> getSqlFiledForStr(String str) {
        Select select;
        HashMap hashMap = new HashMap();
        try {
            select = (Select) new CCJSqlParserManager().parse(new StringReader(str));
        } catch (JSQLParserException e) {
            e.printStackTrace();
            select = null;
        }
        SelectBody selectBody =  select.getSelectBody();
        new ArrayList();
        if (selectBody instanceof SetOperationList) {
            List selects = ((SetOperationList) selectBody).getSelects();
            for (int i = 0; i < selects.size(); i++) {
                listToMap(hashMap, analySqlFiled((PlainSelect) selects.get(i)));
            }
        }
        if (selectBody instanceof PlainSelect) {
            listToMap(hashMap, analySqlFiled((PlainSelect) selectBody));
        }
        return hashMap;
    }

    private static void listToMap(Map<String, Object> map, List<String> list) {
        for (String str : list) {
            if (!"*".equals(str)) {
                map.put(str, str);
            }
        }
    }

    public static List<String> analySqlFiled(PlainSelect plainSelect) {
        String obj;
        List<SelectItem> selectItems = plainSelect.getSelectItems();
        ArrayList arrayList = new ArrayList();
        if (selectItems != null) {
            for (SelectItem selectExpressionItem : selectItems) {
                if (selectExpressionItem instanceof SelectExpressionItem) {
                    SelectExpressionItem selectExpressionItem2 = (SelectExpressionItem) selectExpressionItem;
                    Alias alias = selectExpressionItem2.getAlias();
                    Expression expression = selectExpressionItem2.getExpression();
                    if (expression instanceof CaseExpression) {
                        obj = alias.getName();
                    } else if ((expression instanceof LongValue) || (expression instanceof StringValue) || (expression instanceof DateValue) || (expression instanceof DoubleValue)) {
                        if (Objects.nonNull(alias.getName())) {
                            obj = alias.getName();
                        } else {
                            obj = expression.getASTNode().jjtGetValue().toString();
                        }
                    } else if (expression instanceof TimeKeyExpression) {
                        obj = alias.getName();
                    } else if (alias != null) {
                        obj = alias.getName();
                    } else {
                        Object jjtGetValue = expression.getASTNode().jjtGetValue();
                        if (jjtGetValue instanceof Column) {
                            obj = ((Column) jjtGetValue).getColumnName();
                        } else if (jjtGetValue instanceof Function) {
                            obj = jjtGetValue.toString();
                        } else {
                            obj = String.valueOf(jjtGetValue).replace(CgformUtil.f229C, "").replace("\"", "").replace("`", "");
                        }
                    }
                    arrayList.add(obj.replace(CgformUtil.f229C, "").replace("\"", "").replace("`", ""));
                } else if (selectExpressionItem instanceof AllTableColumns) {
                    arrayList.add(((AllTableColumns) selectExpressionItem).toString());
                } else {
                    arrayList.add(selectExpressionItem.toString());
                }
            }
        }
        return arrayList;
    }
}
